package com.alibaba.shopping_order_customer_api.controller;

import com.alibaba.shopping_common.pojo.Address;
import com.alibaba.shopping_common.pojo.Area;
import com.alibaba.shopping_common.pojo.City;
import com.alibaba.shopping_common.pojo.Province;
import com.alibaba.shopping_common.result.BaseResult;
import com.alibaba.shopping_common.service.AddressService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.security.PublicKey;
import java.util.List;

/**
 * @ClassName AddressController
 * @Description TODO
 * @Author Leo
 * @Date 2024/12/18 19:10
 */
@RestController
@RequestMapping("/user/address")
public class AddressController {

    @DubboReference
    private AddressService addressService;

    /**
     * 查询所有的省份信息
     *
     * @return
     */
    @GetMapping("/findAllProvince")
    public BaseResult<List<Province>> findAllProvince() {
        List<Province> allProvince = addressService.findAllProvince();
        return BaseResult.OK(allProvince);
    }

    /**
     * 根据省份查询所有的城市信息
     *
     * @param provinceId
     * @return
     */
    @GetMapping("/findCityByProvince")
    public BaseResult<List<City>> findCityByProvince(Long provinceId) {
        List<City> cityList = addressService.findCityByProvince(provinceId);
        return BaseResult.OK(cityList);
    }

    /**
     * 根据城市ID查询所有的区县
     *
     * @param cityId
     * @return
     */
    @GetMapping("/findAreaByCity")
    public BaseResult<List<Area>> findAreaByCity(Long cityId) {
        List<Area> areas = addressService.findAreaByCity(cityId);
        return BaseResult.OK(areas);
    }

    /**
     * 新增收货地址
     *
     * @param address
     * @return
     */
    @PostMapping("/add")
    public BaseResult<Address> add(@RequestBody Address address) {
        // 由于higress网关无法调用本地服务，所以默认对userId=29进行操作
        address.setUserId(29L);
        addressService.addAddress(address);
        return BaseResult.OK(address);
    }

    /**
     * 根据ID更新用户收货地址
     *
     * @param address
     * @return
     */
    @PutMapping("/update")
    public BaseResult<Address> update(@RequestBody Address address) {
        // 由于higress网关无法调用本地服务，所以默认对userId=29进行操作
        address.setUserId(29L);
        addressService.updateAddress(address);
        return BaseResult.OK(address);
    }

    /**
     * 根据用户ID查询收货地址
     * @param id 用户ID
     * @return
     */
    @GetMapping("/findById")
    public BaseResult<Address> findById(@RequestParam(name = "id",
            required = false, defaultValue = "29") Long id) {
        Address address = addressService.findById(id);
        return BaseResult.OK(address);
    }

    /**
     * 根据ID删除收货地址
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public BaseResult<Long> delete(@RequestParam(name = "id",
            required = false, defaultValue = "29") Long id){
        addressService.delAddress(id);
        return BaseResult.OK(id);
    }

    /**
     * 根据用户ID查询所有的收货地址
     * @param id 用户ID
     * @return
     */
    @GetMapping("/findByUser")
    public BaseResult<List<Address>> findByUser(@RequestParam(name = "id",
            required = false, defaultValue = "29")Long id){
        List<Address> addressList = addressService.findByUserId(id);
        return BaseResult.OK(addressList);
    }

}



